﻿/********************************************************************************/
// <copyright file="BlocksRepository.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
//
// <history>
//  <change who="Hieu Nguyen" date="11/12/2012 2:26:53 PM">Created</change>
// <history>
/********************************************************************************/

namespace JLL.China.Infrastructure.Repositories.Blocks
{

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using JLL.China.Model;
    using System.Data.Linq;

   public  class BlocksRepository :SQLRepositoryBase<Model.Blocks,int>,IBlocksRepository
    {

       public override Model.Blocks FindBy(int key)
       {
           var newsItem = DataContext.lkup_blocks.SingleOrDefault(x => key.Equals(x.idstaticblock));
           if (newsItem != null)
           {
               return BlocksFactory.FromLINQObject(newsItem);
           }
           else
           {
               return null;
           }
       }
       
       public Model.Blocks FindByStatus(int key, NewsStatus status)
       {
           LINQ2SQL.lkup_block newsItem = null;
           if (status.Equals(NewsStatus.Undefined))
           {
               return FindBy(key);
           }
           else
           {
               bool _status = true;
               if(status.Equals(NewsStatus.Hide))
               {
                   _status = false;
               }
               newsItem = DataContext.lkup_blocks.SingleOrDefault(x => key.Equals(x.idstaticblock) && x.status ==_status);
           }

           if (newsItem != null)
           {
               return BlocksFactory.FromLINQObject(newsItem);
           }
           else 
           {
               return null;
           }
       }

       public override IList<Model.Blocks> FindAll()
       {
           throw new NotImplementedException();
       }

       protected override void PersistNewItem(Model.Blocks item)
       {
           DataContext.lkup_blocks.InsertOnSubmit(BlocksFactory.ToLINQObject(item));
       }

       protected override void PersistUpdatedItem(Model.Blocks item)
       {
           var existNewsItem = DataContext.lkup_blocks.SingleOrDefault(x => item.ID.Equals(x.idstaticblock));
           if (existNewsItem != null)
           {
               existNewsItem.idstaticblock = item.ID;
               
               existNewsItem.title = item.Title;
               existNewsItem.slug = item.Slug;
               existNewsItem.block_content = item.BlockContent;
               existNewsItem.system_block = item.SystemBlock;
               existNewsItem.status = item.Status;
              
           }
           else
           {
               throw new Exception("NewsItem not found to update");
           }
       }

       protected override void PersistDeletedItem(Model.Blocks item)
       {
           var existNewsItem = DataContext.lkup_blocks.SingleOrDefault(x => item.ID.Equals(x.idstaticblock));
           if (existNewsItem != null)
           {
               DataContext.lkup_blocks.DeleteOnSubmit(existNewsItem);
           }
       }

        public IList<Model.Blocks> Search()
        {
            var items = SearchByCriteria();
            //default
            items = items.OrderBy(x => x.idstaticblock);
            //
            var list = items.ToList();

            var result = new List<Model.Blocks>();

            foreach (var n in list)
            {
                result.Add(BlocksFactory.FromLINQObject(n));
            }
            return result;
        }
        public int CountBlocksItem()
        {
            var items = SearchByCriteria();
            return items.Count();
        }
        private IQueryable<LINQ2SQL.lkup_block> SearchByCriteria()
        {
            var items = from m in DataContext.lkup_blocks
                        select m;

            return items;
        }
       //test 
        public int Square(int a1)
        {
            return a1 * a1;
        }
        public int AddNumbers(int a1, int b1)
        {
            return a1 + b1;
        }
    }
}
